#!/bin/bash

set -e

[ -e ../../build.py ] || exit 1;
rm -rf bin
rm -rf .git

cleanup_exit() {
  echo "Test failed"
  rm -rf .git
  rm -f dummy_file
  exit ${1}
}

git init
echo 1 > dummy_file
git add dummy_file
git commit -m "c1"

echo "Build should succeed"
../../build.py build :main || cleanup_exit 1
[ $(bin/main) == $(git rev-parse HEAD) ] || cleanup_exit 1
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph1 - || cleanup_exit 1

echo 2 > dummy_file
git add dummy_file
git commit -m "c2"

echo "Build should succeed"
../../build.py build :main || cleanup_exit 1
[ $(bin/main) == $(git rev-parse HEAD) ] || cleanup_exit 1
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph2 - || cleanup_exit 1

git checkout -b test_branch
echo 3 > dummy_file
git add dummy_file
git commit -m "c3"

echo "Build should succeed"
../../build.py build :main || cleanup_exit 1
[ $(bin/main) == $(git rev-parse HEAD) ] || cleanup_exit 1
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph3 - || cleanup_exit 1

rm -rf bin
rm -rf .git
rm -f dummy_file

echo 'Test passed'
